package com.teaching.teachingsupport.controller.administratorcontroller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.teaching.teachingsupport.common.pojo.dto.request.CreateUserRequest;
import com.teaching.teachingsupport.common.pojo.dto.response.AmountRequest;
import com.teaching.teachingsupport.common.pojo.dto.response.UserPageResponse;
import com.teaching.teachingsupport.common.utils.BeanCover;
import com.teaching.teachingsupport.service.service2.UserService;
import com.teaching.teachingsupport.service.service2.Administratorservice.TeacherAmountService;
import com.teaching.teachingsupport.service.service2.Administratorservice.StudentAmountService;
import com.teaching.teachingsupport.common.pojo.dto.response.TeacherDTO;
import com.teaching.teachingsupport.service.service2.Administratorservice.CourseManagementService;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/administrator")
public class AdsHomePageController {

    @Autowired
    private UserService userService;
    
    @Autowired
    private TeacherAmountService teacherAmountService;
    
    @Autowired
    private StudentAmountService studentAmountService;

    @Autowired
    private CourseManagementService courseManagementService;

    @RequestMapping("/counts")
    public AmountRequest getSpecificQuantity() {
        // 获取各类数量
        Integer totalUsers = userService.getTotalUserCount();
        Integer teacherCount = teacherAmountService.getTeacherCount();
        Integer studentCount = studentAmountService.getStudentCount();
        Integer activeUsers = userService.getDailyActiveUsers();
        
        // 使用工具类统一设置属性
        return BeanCover.coverAmount(totalUsers, teacherCount, studentCount, activeUsers);
    }

    /**
     * 获取用户列表
     * @param pageNum 页码
     * @param pageSize 每页大小
     * @param roleType 角色类型（可选）
     * @param grade 年级（可选，仅对学生角色有效）
     * @param searchKey 搜索关键字（可选，可以是账号名或用户姓名）
     * @return 分页用户数据
     */
    @GetMapping("/users")
    public Page<UserPageResponse> getUserDirectory(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize,
            @RequestParam(required = false) String roleType,
            @RequestParam(required = false) String grade,
            @RequestParam(required = false) String searchKey) {
        return userService.getUserPage(pageNum, pageSize, roleType, grade, searchKey);
    }

    /**
     * 创建新用户
     */
    @RequestMapping("/createUsers")
    public Boolean createUser(@RequestBody @Valid CreateUserRequest request) {
        return userService.createUser(request);
    }

    /**
     * 搜索教师
     * @param teacherNoOrName 教师工号或姓名（可选）
     * @param courseName 课程名称（可选）
     * @param status 在职状态（可选，"在职"/"离职"）
     * @return 教师列表
     */
    @GetMapping("/teachers/search")
    public List<TeacherDTO> searchTeachers(
            @RequestParam(required = false) String teacherNoOrName,
            @RequestParam(required = false) String courseName,
            @RequestParam(required = false) String status) {
        return courseManagementService.searchTeachers(teacherNoOrName, courseName, status);
    }

    /**
     * 创建新课程
     * @param request 课程创建请求
     * @return 创建是否成功
     */
//    @PostMapping("/course/create")
//    public Boolean createCourse(@RequestBody @Valid CreateCourseRequest request) {
//        return courseManagementService.createCourse(request);
//    }

    /**
     * 更新教师信息
     * @param teacherId 教师ID
     * @param request 更新请求
     * @return 更新是否成功
     */
//    @RequestMapping("/teacher/{teacherId}")
//    public Boolean updateTeacher(@PathVariable Integer teacherId, @RequestBody TeacherDTO request) {
//        return courseManagementService.updateTeacher(teacherId, request);
//    }
}
